const axios = require('axios');
const debug = require('debug')('app:chat');

const API_SECRET = 'RUSyIQGxecdrhgQJtuaW:LHfFGEsnqnGFxWslVpGo'
const API_URL = 'https://spark-api-open.xf-yun.com/v1/chat/completions'; // 替换为实际的API URL

const createChat = async (req, res) => {
 try {
    debug('User created successfully: %O', req.body);
    console.log('chat--------------------------------------:', req.body)
    const response = await axios.post(API_URL, 
        {
            "model": "4.0Ultra", // lite、 generalv3、 pro-128k、 generalv3.5、 max-32k、 4.0Ultra
            "user": "7162207b",
            ...req.body
        },
        {
            headers: {
                'Content-Type': 'application/json',
                'Authorization':'Bearer ' + API_SECRET
            }
        }
    );
    // debug('User created successfully: %O', response.data);
    // console.log('chat--------------------------------------:', response.data)
    // res.end(JSON.stringify(response.data)); // 会乱码
    res.status(200).send(response.data)
    } catch (error) {
        res.status(500).send('chat error:' + error);
    }
};

module.exports = {
  createChat,
};
